package com.eric.controller;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.util.SaResult;
import com.eric.domain.dto.MaterialInDto;
import com.eric.domain.po.Material;
import com.eric.domain.po.MaterialIn;
import com.eric.mapper.MaterialInMapper;
import com.eric.service.IMaterialInService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 材料入库表 前端控制器
 * </p>
 *
 * @author 新哥
 * @since 2024-03-12
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/material-in")

public class MaterialInController {
    private final IMaterialInService materialInService;

    MaterialInMapper materialInMapper;

    //    添加新增加材料表单
    @PostMapping("/saveOne")
    public SaResult saveOne(MaterialInDto materialInDto, MultipartFile[]photoFiles, MultipartFile[] materialFiles ,HttpServletRequest request){
        return materialInService.saveOneService(materialInDto,photoFiles,materialFiles,request);
    }

    //    修改一条表单
    @PutMapping("/modifyOne")
    @SaCheckPermission("sys:modify")
    @SaCheckRole(value = {"sys:coder","sys:superAdmin","sys:admin","sys:manager"},mode = SaMode.OR)
    public SaResult modifyOne(MaterialInDto materialInDto, MultipartFile[]photoFiles, HttpServletRequest request,String[] photoList){



        return materialInService.modifyOneService(materialInDto,photoFiles,request,photoList);
    }

    //    根据id删除一条

    @DeleteMapping("/{id}")
    @SaCheckPermission("sys:delete")
    @SaCheckRole(value = {"sys:coder","sys:superAdmin","sys:admin","sys:manager"},mode = SaMode.OR)
    public SaResult deleteOneById(@PathVariable("id") Integer id){
        return materialInService.deleteOneByIdService(id);
    }

    //    根据多个id批量删除
    @DeleteMapping("/deleteByIds")
    @SaCheckPermission("sys:delete")
    @SaCheckRole(value = {"sys:coder","sys:superAdmin","sys:admin","sys:manager"},mode = SaMode.OR)
    public SaResult deleteByIds(@RequestBody ArrayList<Integer>ids){
        return materialInService.deleteByIdsService(ids);
    }

    //    分页查询所有数据
    @GetMapping("/getAllByPage")
    public SaResult getAllByPage(Integer current,Integer pageSize){
        return materialInService.getAllByPage(current,pageSize);
    }

    //    配合查询所有数据接口，获取所有数据的条数
    @GetMapping("/getAllCounts")
    public SaResult getAllCounts(){
        return materialInService.getAllCountsService();
    }




    //    根据Id查询一条数据
    @GetMapping("/{id}")
    public SaResult getOneById(@PathVariable("id") Integer id){
        return materialInService.getOneByIdService(id);
    }

//    //    根据两个时间段查询
//    @GetMapping("/getByBetweenTimes")
//    public SaResult getAllByBetweenTimes(@DateTimeFormat(iso=DateTimeFormat.ISO.DATE) Date start,@DateTimeFormat(iso=DateTimeFormat.ISO.DATE)Date end, Integer current, Integer pageSize){
//        return materialInService.getByBetweenTimesService(start,end,current,pageSize);
//    }

    //    根据创建时间查询
    @GetMapping("/getByCreateTime")
    public SaResult getByCreateTime(@DateTimeFormat(iso=DateTimeFormat.ISO.DATE) Date createTime,Integer current,Integer pageSize){
        return materialInService.getByCreateTimeService(createTime,current,pageSize);
    }

    //    根据修改时间查询
    @GetMapping("/getByUpdateTime")
    public SaResult getByUpdateTime(@DateTimeFormat(iso=DateTimeFormat.ISO.DATE) Date updateTime,Integer current,Integer pageSize){
        return materialInService.getByUpdateTimeService(updateTime,current,pageSize);
    }

    @GetMapping("/getByMaterialName")
    public SaResult getByMaterialName(String materialName,Integer current,Integer pageSize){
        return materialInService.getByMaterialNameService(materialName,current,pageSize);
    }

    @GetMapping("/getByLastUpdateBy")
    public SaResult getByLastUpdateBy(String lastByName,Integer current,Integer pageSize){
        return materialInService.getByLastUpdateByService(lastByName,current,pageSize);
    }

    @GetMapping("/getByCreateBy")
    public SaResult getByCreateBy(String createByName,Integer current,Integer pageSize){
        return materialInService.getByCreateByService(createByName,current,pageSize);
    }

    @GetMapping("/getAllByMoon")
    public SaResult getAllByMoon(@DateTimeFormat(iso=DateTimeFormat.ISO.DATE) Date moonDate, Integer current,Integer pageSize){
        return materialInService.getAllByMoon(moonDate,current,pageSize);
    }

    //  todo 好好想想  根据材料子类型并根据Number数量排序查询，
    @GetMapping("/getByTypeName")
    public SaResult getByTypeName(String parentType,String childTypeName,Integer orderBy,Integer current,Integer pageSize){
        return materialInService.getByTypeName(parentType,childTypeName,orderBy,current,pageSize);
    }

    //  多功能查询接口，这里面的name包括设备名、创建人，最后修改人，项目名称
    @GetMapping("/getAllByMultifunctionName")
    public SaResult getAllByMultifunctionName(String name,Integer current,Integer pageSize){
        return materialInService.getAllByMultifunctionNameService(name,current,pageSize);
    }
    // 配合多功能查询接口，获取到所有条数
    @GetMapping("/getAllCountsByMultifunctionName")
    public SaResult getAllCountsByMultifunctionName(String name,Integer current,Integer pageSize){
        return materialInService.getAllCountsByMultifunctionNameService(name,current,pageSize);
    }
    @GetMapping("/getByBetweenTimes")
    public SaResult getByBetweenTimes(@DateTimeFormat(iso=DateTimeFormat.ISO.DATE)Date start, @DateTimeFormat(iso=DateTimeFormat.ISO.DATE)Date end, Integer current, Integer pageSize){
        return materialInService.getByBetweenTimesService(start,end,current,pageSize);
    }
    @GetMapping("/getAllCountsByBetweenTimes")
    public SaResult getAllCountsByBetweenTimes(@DateTimeFormat(iso=DateTimeFormat.ISO.DATE)Date start, @DateTimeFormat(iso=DateTimeFormat.ISO.DATE)Date end){
        return materialInService.getAllCountsByBetweenTimes(start,end);
    }

    @PostMapping("/saveNewOne")
    public SaResult saveNewOne(MaterialInDto materialInDto, MultipartFile[]photoFiles, HttpServletRequest request) throws IOException {


        return materialInService.saveNewOneService(materialInDto,photoFiles,request);
    }

    @GetMapping("/getMaterialInListByMaterialId")
    public SaResult getOneMaterialInList(boolean timeStatus, Integer materialId,Integer current,Integer pageSize,String projectName,@DateTimeFormat(iso=DateTimeFormat.ISO.DATE)Date start, @DateTimeFormat(iso=DateTimeFormat.ISO.DATE)Date end){
        System.out.println(start);
        System.out.println(end);

        return materialInService.getMaterialInListByMaterialId(timeStatus,materialId,current,pageSize,projectName,start,end);
    }

    @GetMapping("/getMaterialInListByMaterialIdCounts")
    public SaResult getOneMaterialInListCount(boolean timeStatus,Integer materialId,String projectName,@DateTimeFormat(iso=DateTimeFormat.ISO.DATE)Date start, @DateTimeFormat(iso=DateTimeFormat.ISO.DATE)Date end){
        return  materialInService.getMaterialInListCountByMaterialId(timeStatus,materialId,projectName,start,end);
    }





    @PostMapping("/downLoadFiles")
    public void downloadFiles(@RequestBody String[]files, HttpServletResponse response)throws IOException {
        materialInService.downloadFilesService(files, response);
    }

}
